package com.bawei.mall.search.service.impl;

import com.bawei.common.core.constant.UserConstants;
import com.bawei.mall.search.domain.Po.User;
import com.bawei.mall.search.mapper.UserMapper;
import com.bawei.mall.search.service.UserService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @Author：liurui
 * @Package：com.bawei.mall.search.service.impl
 * @Project：mall_cloud
 * @name：UserServiceImpl
 */
@Service
@Log4j2
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper mapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public User findUser() {
        String username = redisTemplate.opsForValue().get(UserConstants.USERNAME);

        try {
            // 调用mapper的方法获取用户信息
            User user = mapper.findUser(username);

            if (user == null) {
                log.warn("User not found.");
                return null;
            }
            // 返回找到的用户信息
            return user;

        } catch (Exception e) {
            log.error("Error occurred while finding user", e);
            return null;
        }
    }
}
